package com.advertise.tool;
  
   
import org.apache.commons.logging.Log;    
import org.apache.commons.logging.LogFactory;    
import org.hibernate.FlushMode;    
import org.hibernate.Session;    
import org.hibernate.SessionFactory;    
import org.hibernate.stat.Statistics;    
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;    
import org.springframework.context.ApplicationContext;    
import org.springframework.context.support.AbstractApplicationContext;    
import org.springframework.orm.hibernate3.SessionFactoryUtils;    
import org.springframework.orm.hibernate3.SessionHolder;    
import org.springframework.transaction.support.TransactionSynchronizationManager;    
   
/**   
 *    
 * 简介: ServicesLocator 是一个静态类,用于从spring 中配置文件中取出需要的类(起代替 spring 的 ApplicationContext 的功能)   
 *   
 * @author ygj   
 * @version 1.0 2005-10-27   
 */   
public final class ServiceLocator    
{    
    private static ServiceLocator instance=new ServiceLocator();    
        
    private static Log log = LogFactory.getLog(ServiceLocator.class.getName());      
    /**   
     * 创建 一个 ServiceLocator类的实例   
     */   
    private ServiceLocator()    
    {    
    }    
        
    /** spring 的 ApplicationContext类 */   
    private ApplicationContext context=null;    
        
    /**    
     * getter 方法    
     */   
    private ApplicationContext getContext()    
    {    
        return context;    
    }    
   
    /**   
     * 设置这个 sping 的 beanFactory 实例   
     * @param context   
     */   
    public void setContext(ApplicationContext context)    
    {    
        this.context = context;    
    }    
   
   
    /**   
     * 得到ServiceLocator类的实例   
     *    
     * @return ServiceLocator类的实例   
     */   
    public static ServiceLocator getInstance()    
    {    
        return instance;    
    }    
   
    /**   
     * 从spring 配置文件中取得一个bean的实例   
     *    
     * @param service   
     *          配置文件中定义bean的实例名称的字符串   
    *     
     * @return   
     *        字符串对应的 bean 的实例   
     *    
     * @since  1.0  2005-10-27   
    */   
    public Object getService(final String service)    
    {    
        return context.getBean(service);    
    }    
        
    public void openHibernateSession()    
   {    
        if(context==null)    
        {    
           log.error("spring ApplicationContext not initially, must first completed it");    
        }    
        SessionFactory sessionFactory = (SessionFactory)ServiceLocator.getInstance().getService("sessionFactory");       
       Session session = SessionFactoryUtils.getSession(sessionFactory, true);       
        session.setFlushMode(FlushMode.AUTO);    
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));       
  
    }    
        
    public void releaseHibernateSession()    
    {    
       if(context==null)    
        {    
            log.error("spring ApplicationContext not initially, must first completed it");    
        }    
        SessionFactory sessionFactory = (SessionFactory)ServiceLocator.getInstance().getService("sessionFactory");    
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);       
        sessionHolder.getSession().flush();    
        SessionFactoryUtils.releaseSession(sessionHolder.getSession(), sessionFactory);    
   }    
  
    /**   
     * 对 已实例的 object 对象的 setter 方法 实现动态注入 (注入为spring 配置文件中定义的bean实体)   
     * @param  已被实例bean对象   
     */   
    public void autowireService(Object bean)    
    {    
        ((AbstractApplicationContext) context).getBeanFactory()    
                .autowireBeanProperties(bean,    
                        AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);    
    }    
        
   
    public Statistics getStatistics()    
    {    
         SessionFactory  sessionFactory=(SessionFactory)ServiceLocator.getInstance().getService("sessionFactory");             Statistics statistics=sessionFactory.getStatistics();    
        return statistics;    
    }    
}    